﻿using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ZoneLinePacking.Core.Extensions {

    /// <summary>
    /// App扩展类
    /// </summary>
    internal static class AppExtensions {

        /// <summary>
        /// 更换皮肤
        /// </summary>
        /// <param name="curApp"></param>
        /// <param name="skin"></param>
        internal static void UpdateSkin(this App curApp, SkinType skin) {
            string resourceStr = string.Empty;
            int index = curApp.Resources.MergedDictionaries.IndexOf(curApp.Resources.MergedDictionaries.FirstOrDefault(x => x.Source != null && x.Source.OriginalString.Contains("Theme/Base"))); ;
            if (index <= -1) {
                return;
            }
            switch (skin) {
                case SkinType.Default:
                    resourceStr = "pack://application:,,,/Resources/Style/Theme/BaseLight.xaml";
                    break;
                case SkinType.Dark:
                    resourceStr = "pack://application:,,,/Resources/Style/Theme/BaseDark.xaml";
                    break;
                default:
                    break;
            }
            ResourceDictionary resource = new ResourceDictionary {
                Source = new Uri(resourceStr)
            };
            curApp.Resources.MergedDictionaries.RemoveAt(index);
            curApp.Resources.MergedDictionaries.Insert(index, resource);
        }

        /// <summary>
        /// 更换语言
        /// </summary>
        /// <param name="curApp"></param>
        /// <param name="langName"></param>
        internal static void UpdateLang(this App curApp, string langName) {

            string resourceStr = string.Empty;
            int index = curApp.Resources.MergedDictionaries.IndexOf(curApp.Resources.MergedDictionaries.FirstOrDefault(x => x.Source != null && x.Source.OriginalString.Contains("Lang/Lang"))); ;
            if (index <= -1) {
                return;
            }
            switch (langName) {
                case "zh-cn":
                    resourceStr = "pack://application:,,,/Resources/Lang/Lang.zh-CN.xaml";
                    break;
                case "en":
                    resourceStr = "pack://application:,,,/Resources/Lang/Lang.en-US.xaml";
                    break;
                default:
                    break;
            }
            ResourceDictionary resource = new ResourceDictionary {
                Source = new Uri(resourceStr)
            };
            curApp.Resources.MergedDictionaries.RemoveAt(index);
            curApp.Resources.MergedDictionaries.Insert(index, resource);
        }

    }
}
